package com.itheima.controller;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Food;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping(path = "/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 上传Excel文件
     *
     * @param excelFile
     * @return
     */
    @PostMapping(path = "/upload")
    public Result upload(MultipartFile excelFile) {
        try {
            //读取excel数据
            List<String[]> excel = POIUtils.readExcel(excelFile);
            //解析excel数据
            List<OrderSetting> orderSettings = excel.stream().map(strings -> {
                OrderSetting orderSetting = new OrderSetting();
                try {
                    orderSetting.setOrderDate(new SimpleDateFormat("yyyy/MM/dd").parse(strings[0]));
                    orderSetting.setNumber(Integer.parseInt(strings[1]));
                } catch (ParseException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e.getMessage());
                }
                return orderSetting;
            }).collect(Collectors.toList());

            //只有当读取的数据不为空 , 才需要导入数据
            if (CollectionUtils.isNotEmpty(orderSettings)) {
                //将文档数据保存到数据库
                orderSettingService.add(orderSettings);
            }

            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    /**
     * 根据月份查询预约信息
     *
     * @param date
     * @return
     */
    @GetMapping(path = "/getOrderSettingsByMonth")
    public Result getOrderSettingsByMonth(String date) {
        try {
            //根据月份查询预约信息
            List<Map<String, Object>> orderSettings = orderSettingService.getOrderSettingsByMonth(date);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, orderSettings);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 设置预约人数
     * @param orderSetting
     * @return
     */
    @PutMapping(path = "/updateOrderSettingNumber")
    public Result updateOrderSettingNumber(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.updateOrderSettingNumber(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);

        }
    }



}
